package com.client.core.service;


import com.client.core.ChatClient;
import com.common.MsgUtil;

import java.io.File;
import java.io.FileInputStream;

public class FileSend implements Runnable {

    private String msg;

    public FileSend(String msg) {
        this.msg = msg;
    }

    @Override
    public void run() {
        //分析文件路径
        String[] split = msg.split("\\.");
        //这个文件的上一层文件夹名字
        String s = split[0] + "." + split[1];
        String wokerpath = System.getProperty("user.dir");
        String s1 = wokerpath + File.separator + "workerFile" + File.separator + "upload";
        String baseFilePath = s1 + File.separator + s + File.separator + msg;
        File file = new File(baseFilePath);
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] data = new byte[inputStream.available()];
            inputStream.read(data);
            inputStream.close();
            ChatClient.channel.writeAndFlush(MsgUtil.SEND_FILE(ChatService.me, ChatService.he, msg.getBytes(), data));
        } catch (Exception e) {
            System.out.println("发送文件失败了" + e.getMessage());
        }
    }

}
